local chenxian = fk.CreateSkill {
  name = "rfenghou_3d15j__chenxian",
  max_round_use_time = 1,
}

chenxian:addEffect("viewas", {
  pattern = "nullification",
  mute_card = false,
  card_num = 2,
  expand_pile = "#rfenghou_3d15j__panjun_haomo",
  card_filter = function(self, player, to_select, selected)
    if player:getPileNameOfId(to_select) == "#rfenghou_3d15j__panjun_haomo" then
      local mark_table = player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo")
      if mark_table == 0 then return false end
      if #selected == 0 then return true end
      local target_cards = table.find(mark_table.list_cards, function(cids) return table.contains(cids, selected[1]) end)
      return table.contains(target_cards or {}, to_select)
    end
  end,
  prompt = "#rfenghou_3d15j__chenxian",
  view_as = function(self, player, cards)
    if #cards ~= 2 then return end
    local c = Fk:cloneCard("nullification")
    c.skillName = chenxian.name
    c:addFakeSubcards(cards)
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local cards = use.card.fake_subcards

    local mark_table = table.clone(player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo"))
    if mark_table == 0 then return "" end
    local list_cards = mark_table.list_cards
    local target_cards = table.find(list_cards, function(cids) return table.contains(cids, cards[1]) end)
    if not target_cards then return "" end
    table.removeOne(list_cards, target_cards)

    local tos, _, _ = room:askToChooseCardsAndPlayers(player, {
      min_card_num = 0,
      max_card_num = 2,
      min_num = 1,
      max_num = 1,
      expand_pile = cards,
      pattern = tostring(Exppattern:new{ id = cards }),
      targets = room:getOtherPlayers(player, false),
      skill_name = chenxian.name,
      prompt = "#rfenghou_3d15j__chenxian-give",
      cancelable = false
    })
    if #tos == 0 then return "" end

    -- 神秘借用extra函数
    local remain = Fk.skill_skels["rfenghou_3d15j__guanwei"].extra.rfenghou_3d15j__setGuanweiMark(player, {
      target = player,
      card_table = list_cards,
      skill_name = chenxian.name,
      skip = true,
    })
    use.extra_data = use.extra_data or {}
    use.extra_data.rfenghou_3d15j__chenxianGiver = tos[1].id
    room:obtainCard(tos[1], remain)
  end,
  after_use = function(self, player, use)
    local room = player.room
    local to = room:getPlayerById((use.extra_data or {}).rfenghou_3d15j__chenxianGiver)
    if to == nil or to:getHandcardNum() < 2 then return end

    local mark_table = table.clone(player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo"))
    if mark_table == 0 then return "" end
    local card_table = mark_table.list_cards

    local to_put_cards = room:askToCards(to, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      cancelable = false,
      skill_name = chenxian.name,
      prompt = "#rfenghou_3d15j__chenxian-put:" .. player.id
    })
    if #to_put_cards == 2 then
      table.insert(card_table, to_put_cards)
    end
    Fk.skill_skels["rfenghou_3d15j__guanwei"].extra.rfenghou_3d15j__setGuanweiMark(to, {
      target = player,
      card_table = card_table,
      skill_name = chenxian.name,
    })
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo") ~= 0 and #Fk:currentRoom().alive_players > 1
  end,
  enabled_at_nullification = function(self, player, data)
    return player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo") ~= 0 and #Fk:currentRoom().alive_players > 1
  end
}, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["rfenghou_3d15j__chenxian"] = "陈险",
  [":rfenghou_3d15j__chenxian"] = "每轮限一次，你可将一组“毫末”交给一名其他角色，视为使用【无懈可击】，然后其将两张牌置为一组“毫末”。",

  ["#rfenghou_3d15j__chenxian"] = "陈险：将一组“毫末”交给一名角色，视为使用【无懈可击】，然后其将两张牌置为一组“毫末”",
  ["#rfenghou_3d15j__chenxian-give"] = "陈险：请将“毫末”交给一名角色，【无懈可击】结算结束后，其须将两张牌置为一组“毫末”",
  ["#rfenghou_3d15j__chenxian-put"] = "陈险：请<font color='red'>有序地</font>选择两张牌置为 %src 的一组“毫末”",
}

return chenxian